package com.glela.common.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ListUtils {
	/**
	 * 获取两个列表的交集  在originalList 中 与referList 交叉的集合
	 * @param originalList
	 * @param referList
	 * @return
	 */
	public static List<String> getUnion(List<String> originalList, List<String> referList) {
		if (originalList == null || originalList.size() <= 0 || referList == null || referList.size() <= 0) {
			return null;
		}
		List<String> res = new ArrayList<String>();
		Map<String, String> map = new HashMap<String, String>();
		for (String tmp : originalList) {
			map.put(tmp, tmp);
		}
		for (String tmp : referList) {
			if (map.containsKey(tmp)) {
				res.add(tmp);
			}
		}
		return res;
	}

	/**
	 * 获取两个set的交集  在originalSet 中 与referSet 交叉的集合
	 * @param originalSet
	 * @param referSet
	 * @return
	 */
	public static List<Long> getSetsUnion(Set<String> originalSet, Set<String> referSet) {
		List<Long> res = new ArrayList<Long>();
		if (originalSet == null || originalSet.size() <= 0 || referSet == null || referSet.size() <= 0) {
			return res;
		}
		Map<String, String> map = new HashMap<String, String>();
		for (String tmp : originalSet) {
			map.put(tmp, tmp);
		}
		for (String tmp : referSet) {
			if (map.containsKey(tmp)) {
				res.add(Long.valueOf(tmp));
			}
		}
		return res;
	}

	/**
	 * 获取两个列表的交集  在originalList 中 与referList 不同的
	 *
	 * @param originalList
	 * @param referList
	 * @return
	 */
	public static List<String> getDifference(List<String> originalList, List<String> referList) {
		if (referList == null || referList.size() <= 0) {
			return originalList;
		}
		if (originalList == null || originalList.size() <= 0) {
			return referList;
		}
		List<String> res = new ArrayList<String>();
		Map<String, String> map = new HashMap<String, String>();
		for (String tmp : originalList) {
			map.put(tmp, tmp);
		}
		for (String tmp : referList) {
			if (!map.containsKey(tmp)) {
				res.add(tmp);
			}
		}
		return res;
	}

	/**
	 * 获取两个set的差异  在originalSet 中 与referSet 不同的
	 *
	 * @param originalSet
	 * @param referSet
	 * @return
	 */
	public static List<Long> getSetsDifference(Set<String> originalSet, Set<String> referSet) {
		Set<String> resultSet = new HashSet<>();
		if (referSet == null || referSet.size() <= 0) {
			resultSet = originalSet;
		} else if (originalSet == null || originalSet.size() <= 0) {
			resultSet = referSet;
		} else {
			Map<String, String> map = new HashMap<String, String>();
			for (String tmp : originalSet) {
				map.put(tmp, tmp);
			}
			for (String tmp : referSet) {
				if (!map.containsKey(tmp)) {
					resultSet.add(tmp);
				}
			}
		}

		List<Long> result = new ArrayList<>();
		if (referSet != null && resultSet.size() > 0) {
			for (String s : resultSet) {
				result.add(Long.valueOf(s));
			}
		}
		return result;
	}
	
	/** originalList 删除 包含  referList
	 * @param originalList
	 * @param referList
	 * @return
	 */
	public static List<String> removeList(List<String> originalList,List<String> referList){
		if(originalList == null ||originalList.size()<=0 || referList == null ||referList.size()<=0 ){
			return originalList;
		}
		 originalList.removeAll(referList);
		 return originalList;
	};
	
	/**判断集合是否为空
	 * @param collection
	 * @return
	 */
	public static boolean isNotEmpty(Collection<?>  collection){
 		 if(collection!=null && collection.size()>0){
			 return true;
		 }
		 return false;
	}
 }
